INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE(PkgConfig)
PKG_CHECK_MODULES(PKG_LIBXML2 libxml-2.0)

IF(PKG_LIBXML2_FOUND)
    SET(LIBXML2_INCLUDE_DIRS ${PKG_LIBXML2_INCLUDE_DIRS})
    IF(BUILD_SHARED_LIBS)
        SET(LIBXML2_LIBRARIES ${PKG_LIBXML2_LIBRARIES})
    ELSE(BUILD_SHARED_LIBS)
        SET(LIBXML2_LIBRARIES ${PKG_LIBXML2_STATIC_LIBRARIES})
    ENDIF(BUILD_SHARED_LIBS)

ELSE(PKG_LIBXML2_FOUND)
    FIND_PATH(LIBXML2_INCLUDE_DIRS libxml/tree.h
        PATHS
        ${PKG_LIBXML2_INCLUDE_DIRS}
        /usr/include
        /usr/local/include
    )

    FIND_LIBRARY(LIBXML2_LIBRARIES xml2
        ${PKG_LIBXML2_LIBRARY_DIRS}
        /usr/lib
        /usr/local/lib
    )

    FIND_PACKAGE_HANDLE_STANDARD_ARGS(LibXml2 DEFAULT_MSG LIBXML2_LIBRARIES LIBXML2_INCLUDE_DIRS)
ENDIF(PKG_LIBXML2_FOUND)

IF(NOT BUILD_SHARED_LIBS)
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DLIBXML_STATIC")
ENDIF()